//
//  JSViewController43.swift
//  RXSwiftTest
//
//  Created by ZhangBo on 2019/6/18.
//  Copyright © 2019 ZhangBo. All rights reserved.
//

import UIKit
import RxSwift
import RxCocoa
import RxDataSources


//#MARK: https://www.jianshu.com/p/a6c0c607ae0c

class JSViewController43: BaseViewController {

    let disposeB = DisposeBag()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "URLSession的使用1：请求数据"
        addNavBar()
        test5()
    }
}

//1，通过 rx.response 请求数据

extension JSViewController43 {

    fileprivate func test1() {
        //创建URL对象
        let urlString = "https://www.douban.com/j/app/radio/channels"
        let url = URL(string: urlString)
        //创建请求对象
        let request = URLRequest(url: url!)

        //创建并发起请求
        URLSession.shared.rx.response(request: request).subscribe(onNext: {
            (response, data) in
            //数据处理
            let str = String(data: data, encoding: String.Encoding.utf8)
            print("返回的数据是：", str ?? "")
        }).disposed(by: disposeB)
    }

    fileprivate func test2() {
        //创建URL对象
        let urlString = "https://www.douban.com/xxxxxxxxxx/app/radio/channels"
        let url = URL(string: urlString)
        //创建请求对象
        let request = URLRequest(url: url!)

        //创建并发起请求
        URLSession.shared.rx.response(request: request).subscribe(onNext: {
            (response, data) in
            //判断响应结果状态码
            if 200..<300 ~= response.statusCode {
                let str = String(data: data, encoding: String.Encoding.utf8)
                print("请求成功！返回的数据是：", str ?? "")
            } else {
                print("请求失败！")
            }
        }).disposed(by: disposeB)
    }
}

//2，通过 rx.data 请求数据

extension JSViewController43 {
    fileprivate func test3() {
        //创建URL对象
        let urlString = "https://www.douban.com/j/app/radio/channels"
        let url = URL(string: urlString)
        //创建请求对象
        let request = URLRequest(url: url!)

        //创建并发起请求
        URLSession.shared.rx.data(request: request).subscribe(onNext: {
            data in
            let str = String(data: data, encoding: String.Encoding.utf8)
            print("请求成功！返回的数据是：", str ?? "")
        }).disposed(by: disposeB)
    }

    fileprivate func test4() {
        //创建URL对象
        let urlString = "https://www.douban.com/xxxxxx/app/radio/channels"
        let url = URL(string: urlString)
        //创建请求对象
        let request = URLRequest(url: url!)

        //创建并发起请求
        URLSession.shared.rx.data(request: request).subscribe(onNext: {
            data in
            let str = String(data: data, encoding: String.Encoding.utf8)
            print("请求成功！返回的数据是：", str ?? "")
        }, onError: { error in
            print("请求失败！错误原因：", error)
        }).disposed(by: disposeB)
    }
}

//3 二、手动发起请求、取消请求
extension JSViewController43 {
    fileprivate func test5() {
        //创建URL对象
        let urlString = "https://www.douban.com/j/app/radio/channels"
        let url = URL(string: urlString)
        //创建请求对象
        let request = URLRequest(url: url!)

        navigationBar.setTitles("开始", "取消")
        //发起请求按钮点击
        navigationBar.buttonA.rx.tap.asObservable().flatMap {
                URLSession.shared.rx.data(request: request).takeUntil(self.navigationBar.buttonB.rx.tap) //如果“取消按钮”点击则停止请求
            }
            .subscribe(onNext: { data in
                let str = String(data: data, encoding: String.Encoding.utf8)
                print("请求成功！返回的数据是：", str ?? "")
            }, onError: { error in
                print("请求失败！错误原因：", error)
            }).disposed(by: disposeB)
    }
}
